package com.sevici;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.Typeface;
 
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;
import com.sevici.model.Estacion;

public class CustomOverlay extends Overlay {
	private static final int CIRCLERADIUS = 2;
	private Estacion estacion;
	private Canvas canvasObjeto;
	
	public CustomOverlay(Estacion elem) {
		//geopoint = elem.getGeoLocalizacion();
		estacion = elem;
	}
 
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		//String cadenaEstacion = estacion.getAvailable().concat("/").concat(estacion.getFree())
		canvasObjeto = canvas;
		
		// Transfrom geoposition to Point on canvas
		Projection projection = mapView.getProjection();
		Point point = new Point();
		projection.toPixels(estacion.getGeoLocalizacion(), point);
		
		int bgColor;
		// colores por disponibilidad
		if(Integer.parseInt(estacion.getAvailable()) < 5) {
			bgColor = Color.argb(80, 255, 0, 0);
		} else {
			bgColor= Color.argb(80, 0, 255, 0);
		}
		
		dibujaCuadro(point.x, point.y, Color.WHITE, bgColor, estacion.getAvailable());
		dibujaCuadro(point.x + 20, point.y, Color.WHITE, Color.argb(80, 200, 0, 0), estacion.getFree());
		
		
		// the circle to mark the spot
		Paint circle = new Paint();
		circle.setColor(Color.BLACK);
		circle.setAntiAlias(true);
		canvas.drawCircle(point.x, point.y, CIRCLERADIUS, circle);
	}
	
	private void dibujaCuadro(int x, int y, int color, int background, String texto){
		dibujaCuadro(x, y, color, background, texto, 0, 0);
	}
	
	private void dibujaCuadro(int x, int y, int color, int background, String texto, int overrideX, int overrideY){
		// BORNETAS LIBRES
		// background
		//float widths[] = null;
		Paint backgroundPaint = new Paint();
		backgroundPaint.setColor(background);
		
		// texto
		Paint textoPaint = new Paint();
		textoPaint.setAntiAlias(true);
		textoPaint.setColor(color);
		textoPaint.setTextSize(10);
		textoPaint.setTypeface(Typeface.MONOSPACE);
		/*
		textoPaint.getTextWidths(texto, widths);
		
		float totalWidth = 0; 
		for(float w : widths) {
			totalWidth += w;
		}*/
		// pintamos rectangulo
		RectF rectangulo = new RectF();
		rectangulo.set(overrideX + (x + 2 * CIRCLERADIUS), overrideY + (y - 4 * CIRCLERADIUS), x + 5 + texto.length() * 8, y + 10);
		//rectangulo.set(overrideX + (x + 2 * CIRCLERADIUS), overrideY + (y - 4 * CIRCLERADIUS), x + totalWidth, y + 10);
		canvasObjeto.drawRoundRect(rectangulo, CIRCLERADIUS, CIRCLERADIUS, backgroundPaint);
		
		
		canvasObjeto.drawText(texto, x + 3 * CIRCLERADIUS, y + 3 * CIRCLERADIUS, textoPaint);
	}
	
}
